package com.t9101DR.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.t9101DR.bean.RoomAddress;
import com.t9101DR.bean.order;
import com.t9101DR.dao.BaseDao;
import com.t9101DR.dao.orderDao;

public class orderDaoImpl extends BaseDao implements orderDao {

	@Override
	public List<order> list(order o) {
		Connection conn=null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		List<order> list=new ArrayList<order>();
		try {
			conn=getConnection();
			String sql="SELECT orderId,orderAddress,orderName,orderTime,orderPhone FROM `order` WHERE 1=1";
			if(o!=null&&o.getOrderId()!=0) {
				sql=sql+" and orderId= "+o.getOrderId()+" ";
			}
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()) {
				order bren=new order();
				bren.setOrderId(rs.getInt(1));
				bren.setOrderAddress(rs.getString(2));
				bren.setOrderName(rs.getString(3));
				bren.setOrderTime(rs.getString(4));
				bren.setOrderPhone(rs.getInt(5));
				
				list.add(bren);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			closeAll(conn, pstmt, rs);
		}
		return list;
	}

	@Override
	public int add(order o) {
		int count =0;
		String sql="INSERT INTO order(orderAddress,orderName,orderTime,orderPhone) VALUES (?,?,?,?) ";
		Object [] pare= {o.getOrderAddress(),o.getOrderName(),o.getOrderTime(),o.getOrderPhone()};
		count=executeUpdate(sql, pare);
		return count;
	}

	@Override
	public int update(order o) {
		int count =0;
		String sql=" UPDATE order set orderAddress=?,orderName=?,orderTime=?,orderPhone=? WHERE orderId=?  ";
		Object [] pare= {o.getOrderAddress(),o.getOrderName(),o.getOrderTime(),o.getOrderPhone()};
		count=executeUpdate(sql, pare);
		return count;
	}

	@Override
	public int deleteById(int id) {
		int count=0;
		String sql="delete from order where orderId=?";
		Object[] params= {id};
		count=this.executeUpdate(sql, params);
		return count;
	}

}
